package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.zzw.*;
import com.ruoyi.system.service.ITEmphasisPeopleService;
import com.ruoyi.system.service.ITEventManageService;
import com.ruoyi.system.service.ITPeopleLinkEventService;
import com.ruoyi.system.service.ITRegionInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Description
 * @Author azoveh
 * @Date $ $
 **/


@RestController
@RequestMapping("/screen/test/event")
public class EventDataController {

    @Autowired
    private ITEventManageService eventManageService;
    @Autowired
    private ITRegionInfoService regionInfoService;
    @Autowired
    private ITEmphasisPeopleService emphasisPeopleService;
    @Autowired
    private ITPeopleLinkEventService peopleLinkEventService;


    Random random = new Random();
    List<TRegionInfo> regionInfoList = new ArrayList<>();


    @GetMapping("/addEvent")
    public AjaxResult addEvent() {
        for (int i = 0; i < 100; i++) {


            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            TRegionInfo region = getRegion();
            String type = getType();
            String status = getStatus();
            List<String> levels = Arrays.asList("一级", "二级", "三级", "四级");
            int indexLevel = random.nextInt(levels.size());
            String level = levels.get(indexLevel);
            Date time = getTime();


            String eventName = region.getCRegionName() + formatter.format(time) + level + type;

            TEventManage eventManage = new TEventManage();
            eventManage.setcEventName(eventName);
            eventManage.setcEventDescribe(eventName);
            eventManage.setcEventCategory(level);
            eventManage.setcEventType(type);
            eventManage.setcEventStatus(status);
            eventManage.setcEventTime(time);
            eventManage.setcEventLocation(region.getCRegionName());
            if ("已处置".equals(status)) {
                eventManage.setcEventCompletedTime(addTime(time));
            }
            eventManage.setcEventDistrictId(region.getCRegionId());
            eventManage.setcEventDistrictName(region.getCRegionName());
            eventManage.setcLongitude(region.getCLongitude());
            eventManage.setcLatitude(region.getCLatitude());
            eventManage.setcEventReach(0L);
            eventManage.setcEventInjuriesNum(0L);
            eventManage.setcEventDieNum(0L);
            eventManage.setcDirectEconomicLosses("0");
            eventManage.setcEventVideoUrl("");

            if ("安全生产".equals(eventManage.getcEventType())) {
                eventManage.setcEventReach((long) random.nextInt(5));
                eventManage.setcEventInjuriesNum((long) random.nextInt(2 * (indexLevel + 1)));
                if (indexLevel > 1) {
                    eventManage.setcEventDieNum((long) random.nextInt(2 * (indexLevel + 1)));
                }
                eventManage.setcDirectEconomicLosses(random.nextInt(1000000) + " CNY");
            }
            if ("交通事故".equals(eventManage.getcEventType())) {
                eventManage.setcEventInjuriesNum((long) random.nextInt(2 * (indexLevel + 1)));
                if (indexLevel > 1) {
                    eventManage.setcEventDieNum((long) random.nextInt(2 * (indexLevel + 1)));
                }
            }
            if ("肇事肇祸".equals(eventManage.getcEventType())) {
                eventManage.setcEventInjuriesNum((long) random.nextInt(2 * (indexLevel + 1)));
                if (indexLevel > 1) {
                    eventManage.setcEventDieNum((long) random.nextInt(2 * (indexLevel + 1)));
                }
            }

            eventManageService.save(eventManage);
        }

        return AjaxResult.success();

    }

    @GetMapping("/relatedPeople")
    public AjaxResult relatedPeople() {

        List<TEmphasisPeople> peopleList = emphasisPeopleService.list();
        for (int m = 0; m < 30; m++) {
            TEmphasisPeople people = peopleList.get(m);

//        }
//
//        for (TEmphasisPeople people : peopleList) {
//            List<TPeopleLinkEvent> linkEventList = new ArrayList<>();
//            int nexted = random.nextInt(5);
            for (int i = 0; i < 10; i++) {
                TPeopleLinkEvent linkEvent = new TPeopleLinkEvent();
                linkEvent.setcPeopleId(people.getcPeopleId());
                linkEvent.setcEventId((long) (random.nextInt(500) + 511));
//                linkEventList.add(linkEvent);
                try {
                    peopleLinkEventService.save(linkEvent);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
            }
        }



        return AjaxResult.success();
    }


    private String getType() {
        List<String> eventTypes = Arrays.asList("安全生产", "交通事故", "肇事肇祸", "邻里纠纷", "房产纠纷", "合同纠纷", "债务纠纷");
        int index = random.nextInt(eventTypes.size());
        return eventTypes.get(index);
    }

    private TRegionInfo getRegion() {
        if (CollectionUtils.isEmpty(regionInfoList)) {
            regionInfoList = regionInfoService.list();
        }
        int index = random.nextInt(regionInfoList.size());
        return regionInfoList.get(index);
    }

    private String getStatus() {
        List<String> status = Arrays.asList("待上报", "待处置", "处置中", "已处置");
        int index = random.nextInt(status.size());
        return status.get(index);
    }

    private String getLevel() {
        List<String> status = Arrays.asList("一级", "二级", "三级", "四级");
        int index = random.nextInt(status.size());
        return status.get(index);
    }

    public Date getTime() {

        // 生成一个介于1970年1月1日和当前日期之间的随机毫秒数
        long millis = random.nextLong() % (System.currentTimeMillis() - (2023 * 365 + 24 * 60 * 60 * 1000));

        // 创建一个Calendar实例，并设置日期为1970年1月1日
        Calendar calendar = Calendar.getInstance();
        calendar.set(2023, Calendar.JANUARY, 1);
//        calendar.setTimeInMillis(0);
        // 将随机毫秒数加到日期上
        calendar.add(Calendar.MILLISECOND, (int) millis);
        // 返回生成的随机日期
        return calendar.getTime();
    }

    public Date addTime(Date start) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        // 将随机毫秒数加到日期上
        calendar.add(Calendar.DAY_OF_MONTH, 15);
        // 返回生成的随机日期
        return calendar.getTime();
    }

}
